package cn.hxh;

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.io.File;
import java.io.IOException;
import java.util.List;

import cn.hxh.common.TemplateCollection;

import com.google.common.io.Files;

public class ProtostuffRuntimeTest {

	public static void main(String[] args) {
		try {
			readProto();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void createFile() {
		Schema<Foo> schema = RuntimeSchema.getSchema(Foo.class);
		LinkedBuffer buffer = LinkedBuffer.allocate(256);

		// ser
		try {
			Foo foo = new Foo();
			foo.setId(101);
			foo.setName("hexuhui");
			foo.setAddress("shanghai");
			byte[] protostuff = ProtostuffIOUtil.toByteArray(foo, schema, buffer);

			try {
				Files.write(protostuff, new File("data.pro"));
			} catch (IOException e) {
				e.printStackTrace();
			}

		} finally {
			buffer.clear();
		}
	}

	public static void readProto() throws IOException {
//		Schema<TemplateCollection> schema = RuntimeSchema.getSchema(TemplateCollection.class);
//		TemplateCollection tc = new TemplateCollection();
//		byte[] pomByte = Files.toByteArray(new File("Stage.pro"));
//		ProtostuffIOUtil.mergeFrom(pomByte, tc, schema);
//		List<StageTemplate> tplList = tc.getObjList(StageTemplate.class);
//		System.out.println(tplList);
	}

}
